<template>
	<div class="system-dicManager-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="500px">

			<el-card shadow="hover">
				<el-form :model="state.ruleForm" size="default" label-width="80px">
					<el-row :gutter="35">
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="分类名称" prop="title">
								<el-input v-model="state.ruleForm.title" placeholder="分类名称" clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="分类编码" prop="classCode">
								<el-input v-model="state.ruleForm.classCode" placeholder="分类编码" clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="分类排序" prop="articleSort">
								<el-input v-model="state.ruleForm.articleSort" placeholder="分类排序" clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="所属语言" prop="languageType">
								<el-select clearable v-model="state.ruleForm.languageType">
									<dicItemsSelectOptions dicCode="articleLanguage" data-type="Int" :withNullValue="false" />
									<!-- <el-option label="英文" :value="2"></el-option>
									<el-option label="中文繁体" :value="3"></el-option>
									<el-option label="越南语" :value="4"></el-option>
									<el-option label="日语" :value="5"></el-option>
									<el-option label="韩语" :value="6"></el-option> -->
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="是否启用">
								<el-switch v-model="state.ruleForm.status"></el-switch>
							</el-form-item>
						</el-col>

					</el-row>
				</el-form>


			</el-card>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>

	</div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref, watch,defineAsyncComponent } from 'vue';


const dicItemsSelectOptions = defineAsyncComponent(() => import('/@/components/dic/dicItemsSelectOptions.vue'));

// 导入接口
import { useCommonApi } from '/@/api/common/index'


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);



const state = reactive({
	row: null as any,
	ruleForm: {
		title: "",
		classCode: "",
		status: false,
		languageType: "",
		articleSort: ""
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});



// 打开弹窗
const openDialog = async (type: string, row?: any) => {
	if (type === 'edit') {
		state.row = row;
		state.ruleForm = {
			title: row.title,
			classCode: row.classCode,
			status: row.status == 1 ? true : false,
			languageType: row.languageType,
			articleSort: row.articleSort,
		}


		state.dialog.title = '修改分类';
		state.dialog.submitTxt = '修 改';
	} else {

		state.ruleForm = {
			title: "",
			classCode: "",
			status: false,
			languageType: "",
			articleSort: ""
		}

		state.dialog.title = '新增分类';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};


// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	try {
		if (state.dialog.type == 'edit') {
			let obj = Object.assign({}, {}, state.ruleForm) as any;
			obj.status = obj.status ? 1 : 0;
			obj = Object.assign({}, state.row, obj);
			await useCommonApi().updateArticle(obj);
		} else {
			let obj = Object.assign({}, {}, state.ruleForm) as any;
			obj.status = obj.status ? 1 : 0;
			obj.nodeType = 1;
			obj.parentId = 0;
			await useCommonApi().addArticle(obj);
		}
		ElMessage.success("操作成功");
		closeDialog(); // 关闭弹窗
		emit('refresh');
	} catch (error: any) {
		ElMessage.error("操作失败请稍后再试");
	}
};




watch(
	() => state.dialog.isShowDialog,
	(newValue) => {
		if (newValue == true) {
		}
	}, { immediate: true }
);




// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style lang="scss">
.el-select {
	width: 100% !important;
}



.system-dicManager-dialog-container {
	.bar_tools {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 10px;
	}

	.el-table__expanded-cell {
		padding: 20px;
	}
}
</style>